﻿$(function() {
    /*初始化表格样式*/
    $("#gvData tr").mouseover(function() {
        var curindex = $(this).index();
        if (curindex != 0) {
            $(this).css("background-color", "#E1EEFE");
        }
    })
    $("#gvData tr").mouseout(function() {
        var curindex = $(this).index();
        if (curindex != 0) {
            $(this).css("background-color", "#ffffff");
        }
    })
    /*初始化按钮样式*/
    $(":button,:submit").css("cursor", "pointer");
})

/*
选中checkall时选中所有的复选框
*/
function checkedall( obj )
{
	for( var i=0; i < document.getElementsByName( obj ).length; i++ )
	{
		document.getElementsByName( obj )[ i ].checked = ( ( document.getElementById( "checkall" ).checked == true ) ? true : false );
	}
}

/*
选中obj1时选中所有的复选框
*/
function checkedallE( obj,obj1 )
{
	for( var i=0; i < document.getElementsByName( obj ).length; i++ )
	{
		document.getElementsByName( obj )[ i ].checked = ( ( document.getElementById( obj1 ).checked == true ) ? true : false );
	}
}
	
/*
提示后将选中的复选框的值放入hdIDList的value中
*/
function checkedselect( obj, msg )
{
	var IDList = "";
	for( var i=0; i < document.getElementsByName( obj ).length; i++ )
	{						
		if( document.getElementsByName( obj )[ i ].checked == true )
		{	
			if( IDList == "" ) 
			{
				IDList = document.getElementsByName( obj )[ i ].value;
			}
			else
			{ 
				IDList += "," + document.getElementsByName( obj )[ i ].value;
			}
		}			
	}
		
	if( IDList != "" )
	{ 
	    if(msg=="")
	    {
	       	document.getElementById("hdIDList").value = IDList;
			return true;
	    }
	    else
	    {
		    if( confirm( msg ) )
		    {
			    document.getElementById("hdIDList").value = IDList;
			    return true;
		    }
		    else
		    {
			    return false;
		    }
		}
	}	
	else
	{
		alert( "请先在操作项前打勾!" );
		return false;
	}	
}

/*
提示后将选中的复选框的值放入hobj的value中
*/
function checkedselectE( obj, hobj, msg )
{
	var IDList = "";
	
	for( var i=0; i < document.getElementsByName( obj ).length; i++ )
	{						
		if( document.getElementsByName( obj )[ i ].checked == true )
		{	
			if( IDList == "" ) 
			{
				IDList = document.getElementsByName( obj )[ i ].value;
			}
			else
			{ 
				IDList += "," + document.getElementsByName( obj )[ i ].value;
			}
		}			
	}
		
	if( IDList != "" )
	{
		if( confirm( msg ) )
		{
			document.getElementById(hobj).value = IDList;
			return true;
		}
		else
		{
			return false;
		}
	}	
	else
	{
		alert( "请先在操作项前打勾!" );
		return false;
	}	
}

/*
用于选择多图
*/
function checkedselectandnum(obj, objnum, msg) {
    var IDList = "";
    var NumList = "";
    for (var i = 0; i < document.getElementsByName(obj).length; i++) {
        if (document.getElementsByName(obj)[i].checked == true) {
            if (IDList == "") {
                IDList = document.getElementsByName(obj)[i].value;
            }
            else {
                IDList += "," + document.getElementsByName(obj)[i].value;
            }
            if (NumList == "") {
                NumList = document.getElementsByName(objnum)[i].value;
            }
            else {
                NumList += "," + document.getElementsByName(objnum)[i].value;
            }
        }
    }

    if (IDList != "") {
        if (msg == "") {
            document.getElementById("hdIDList").value = IDList;
            document.getElementById("hdNumList").value = NumList;
            return true;
        }
        else {
            if (confirm(msg)) {
                document.getElementById("hdIDList").value = IDList;
                document.getElementById("hdNumList").value = NumList;
                return true;
            }
            else {
                return false;
            }
        }
    }
    else {
        alert("请先在操作项前打勾!");
        return false;
    }
}

/*
*以下函数弹出一个选择图片的对话框.
*/
function showSelectSinglePic(obj, path, classcode) {
    var returnValue = window.showModalDialog("../UploadManage/Default.aspx?opentype=2&path=" + path + "&classcode=" + classcode, window, "dialogWidth: 900px; dialogHeight: 600px; help: no; scroll: no; status: no");
    if (typeof (returnValue) != "undefined" && returnValue != "undefined") obj.value = returnValue;
}

/*
用于多图上传
*/
function uploadFiles(path, classcode, picdiv, txtbox) {
    var returnValue = window.showModalDialog('../UploadManage/Default.aspx?opentype=4&path=' + path + '&classcode=' + classcode, window, "dialogWidth: 900px; dialogHeight: 600px; help: no; scroll: no; status: no");
    if (returnValue != undefined && returnValue != "returnValue") {
        var items = returnValue.split("^#^");
        for (var i = 0; i < items.length; i++) {
            addImage(items[i], picdiv,txtbox);
        }
    }
    groupFile(txtbox);
}

function addImage(src,picdiv,txtbox) {
    var datas = src.split('|');
    if (datas.length == 4) {
        var truename = datas[2];
        var imgnames = datas[2].split('/');
        var num = imgnames.length;
        if (num > 0) {
            truename = imgnames[num - 1];
        }
        var p_box = document.createElement("div");
        p_box.className = "p_box ui-state-default";
        var p_box_t = document.createElement("div");
        p_box_t.className = "p_box_t";
        var p_box_t_html = "<img src=\"" + datas[1] + "\" width=\"100\" height=\"80\" />";
        p_box_t.innerHTML = p_box_t_html;
        var p_box_m = document.createElement("div");
        p_box_m.className = "p_box_m";
        p_box_m.innerHTML = truename;
        var p_box_b = document.createElement("div");
        p_box_b.className = "p_box_b";
        var p_box_bl = document.createElement("div");
        p_box_bl.className = "p_box_bl";
        var p_box_text = document.createElement("input");
        p_box_text.type = "text";
        p_box_text.className = "p_box_text";
        p_box_text.onblur = function() { groupFile(txtbox); };
        var p_box_br = document.createElement("div");
        p_box_br.className = "p_box_br";
        p_box_br.onclick = function() { delImg(src, txtbox); };
        var hd_p_src = document.createElement("input");
        hd_p_src.type = "hidden";
        hd_p_src.className = "hd_p_src";
        hd_p_src.value = src;
        p_box.appendChild(p_box_t);
        p_box.appendChild(p_box_m);
        p_box_bl.appendChild(p_box_text);
        p_box_b.appendChild(p_box_bl);
        p_box_br.appendChild(hd_p_src);
        p_box_b.appendChild(p_box_br);
        p_box.appendChild(p_box_b);
        document.getElementById(picdiv).appendChild(p_box);
    }
}

function delImg(src, txtbox) {
    $(".hd_p_src").each(function(index) {
        if ($(this).val() == src) {
            var datas = src.split('|');
            if (datas.length == 4) {
                $.ajax({
                    url: '../getdata/fileop.ashx?op=1&src=' + datas[2],
                    type: 'GET',
                    dataType: "json",
                    timeout: 1000,
                    error: function() {
                        alert("调用后台数据失败!");
                    },
                    success: function(data) {
                        if (data.msg != 1) {
                            alert(data.msbox);
                        }
                    }
                });
                $(this).parent().parent().parent().remove();
            }
        }
    })
    groupFile(txtbox);
}

function groupFile(txtbox) {
    var currentVal = "";
    var objs = $(".divupload_b .p_box:visible");
    $.each(objs, function(m) {
        var hdobjs = $(this).find(".hd_p_src");
        var curval = hdobjs.val().split('|');
        if (curval.length == 4) {
            var valpre = curval[0] + "|" + curval[1] + "|" + curval[2] + "|";
            var txtval = $(this).find("input").val();
            if (currentVal == "") {
                currentVal = valpre + +txtval;
            }
            else {
                currentVal += "^#^" + valpre + txtval;
            }
        }
    })
    $("#" + txtbox).val(currentVal);
}

/*缩略图*/
function getNewPic(filename) {
    var paths = filename.split('.');
    var ext = paths[paths.length - 1].toLowerCase();
    var newpic = filename;
    var img = "|jpg|gif|png|icon|bmp|";
    if (img.indexOf("|" + ext + "|") >= 0) {
        newpic = filename.replace("." + ext, "_th." + ext);
    }
    else {
        if (ext == "zip" || ext == "rar")
            newpic = "../images/file/1.gif";
        else if (ext == "swf" || ext == "flv")
            newpic = "../images/file/6.gif";
        else if (ext == "doc")
            newpic = "../images/file/4.gif";
        else if (ext == "xls")
            newpic = "../images/file/5.gif";
        else if (ext == "pdf")
            newpic = "../images/file/3.gif";
        else
            newpic = "../images/file/2.gif";
    }
    return newpic;
}
